/**
 * 
 */
package com.kgcx.schedule.rabbitmq.consumer;

import com.kgcx.common.rabbitmq.consts.ExchangeConst;
import com.kgcx.common.rabbitmq.consts.RoutingKeyConst;
import com.kgcx.core.utils.HelpUtil;
import com.kgcx.schedule.client.City;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 袁进勇
 *
 */
@Component
@Slf4j
public class ScheduleDrvierTaskConsumer extends AbstractTaskConsumer {

    //@RabbitListener(queues = QueueConst.SCHEDULE_SCHEDULE_DRVIER, errorHandler = "custRabbitListenerErrorHandler")

    /**
     * 不再用springCloud 的dataflow来做业务流驱动（由于太占用内存）
     * 把原来由dataflow通过mq发送的请求，直接定义为定时任务
     */
    @Scheduled(cron = "0/5 * * * * ?")  //每5秒钟一次
    public void handler() {
        log.info("收到给订单派车的MQ消息(定时任务)。");

        try {
            List<City> cityList = getCityList();
            if (HelpUtil.isEmpty(cityList)) {
                return;
            }

            // 一个城市发送一个MQ消息
            for (City city : cityList) {
                log.info(String.format("发送给城市[%d]的订单派车的MQ消息。", city.getCityId()));
                rabbitTemplate.convertAndSend(ExchangeConst.SCHEDULE, RoutingKeyConst.SCHEDULE_SCHEDULE_DRVIER,
                        city.getCityId());
            }
        } catch (Exception e) {
            log.error("处理给订单派车的MQ消息失败！", e);
        }
    }
}
